<?php

namespace app\langjinmobile\controller;
//导入基类
use app\common\model\User;
use app\common\model\UserLevel;
use think\Controller;
use think\facade\Cache;
use think\Db;
use app\common\model\Banner;
use app\common\model\Goods as goodsModel;
use think\Request;
use Endroid\QrCode\QrCode;
use app\common\model\IntegralLog as integralLoglModel;
use app\common\model\WalletLog as walletLoglModel;
use app\common\model\GoodsCategory as GoodsCategoryModel;
class Home extends Controller 
{
	public function index(){
		//广告图
        // $active_advs = AdvModel::where('pid=11')->order('sort','asc')->limit(2)->select();
        // $long_advs = AdvModel::where('pid=12')->order('sort','asc')->limit(1)->select();
        // $hot_advs = AdvModel::where('pid=13')->order('sort','asc')->limit(3)->select();
        //公告
        $notice = Db::table('langjin_notice')->where('type',1)->order('update_time desc')->select();

        //轮播图
        $imgs = Banner::order('sort','asc')->select();
        //商品分类
        $goods_category = GoodsCategoryModel::where(['is_show'=>1,'parent_id'=>0])->limit(9)->order('sort_order desc')->select();
        //商品列表
        $goods_list = goodsModel::alias('g')->Join('langjin_business b','b.id = g.bis_id')->where('g.is_on',1)->where('g.amount','>',0)->where('g.bis_id','<>',0)->order('g.id desc')->field(['g.*'])->limit(4)->select();
        $num = goodsModel::alias('g')->Join('langjin_business b','b.id = g.bis_id')->where('g.is_on',1)->where('g.amount','>',0)->where('g.bis_id','<>',0)->count();
        $num = ceil($num/4);
        foreach($goods_list as $key=>$row){
            $img = explode(',',$goods_list[$key]['img1']);
            $goods_list[$key]['img1'] = $img[0];
        }
        //获取当前会员等级
        $user_grade = UserLevel::where('userid',session('userid'))->value('user_grade');
        $this->assign('user_grade',$user_grade);
        $this->assign('goods_list',$goods_list);
        $this->assign('goods_category',$goods_category);
        $this->assign('imgs',$imgs);
        //$this->assign('active_advs',$active_advs);
        //$this->assign('long_advs',$long_advs);
        //$this->assign('hot_advs',$hot_advs);
        $this->assign('notice',$notice);
        
        $this->assign('num',$num);
        return $this->fetch();
    }
    /**
     * 瀑布流加载商品
     */
    public function goodsload(Request $request){
        if($request->isPost()){
            $page = $request->param('page');
            $limit1 = ($page - 1)  * 4;
            $limit2 = 4;
            //GMOB记录
            $goods_list = goodsModel::alias('g')->Join('langjin_business b','b.id = g.bis_id')->where('g.is_on',1)->where('g.amount','>',0)->where('g.bis_id','<>',0)->order('g.id desc')->field(['g.*'])->limit($limit1,$limit2)->select();
            if($goods_list){
                foreach ($goods_list as $key => $val) {
                    $img = explode(',',$goods_list[$key]['img1']);
                    $goods_list[$key]['img1'] = $img[0];
                }
            }

            if(count($goods_list) != 0){
                return array('status'=>1,'data'=>$goods_list);
            }else{
                return array('status'=>0);
            }
        }
    }
    /**
     *  分享
     */
    public function qrcode(Request $request)
    {
        //获取当前用户ID
        $id = session('userid');//获取session中用户的id 来查询UID
        $this->assign('id', $id);
        return $this->fetch();
    }
    /**
     * 二维码
     */
    public function createCode()
    {
        //获取当前用户ID
        $id = session('userid');
        // $qrCode = new QrCode('mobile/index/index?id='.$id);//二维码地址
        $url =$_SERVER['SERVER_NAME'].'/langjinmobile/registered/registered/id/'.$id;
        $qrCode = new QrCode($url);
        $qrCode->setSize(300);

        $qrCode->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0));
        $qrCode->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));
        $qrCode->setLabelFontSize(16);
        header('Content-Type: '.$qrCode->getContentType());
        echo $qrCode->writeString();
        exit;
    }
    /* 设置 */
	public function setting(){
        $user = User::where('id',session('userid'))->field('head_img,nickname,mobile')->find();
        $this->assign('user',$user);
		return $this->fetch(); 
    }
   /* GMOB */
    public function  balance(){
        //GMOB记录
        $wallet = walletLoglModel::where('userid',session('userid'))->order('id desc')->limit(5)->select();
        $this->assign('wallet',$wallet);
		return $this->fetch(); 
    }
    /**
     * 瀑布流加载GMOB记录
     */
    public function walletLog(Request $request){
        if($request->isPost()){
            $page = $request->param('page');
            $limit1 = ($page - 1)  * 5;
            $limit2 = 5;
            //GMOB记录
            $wallet = DB::name('wallet_log')->where('userid',session('userid'))->order('id desc')->limit($limit1,$limit2)->select();
            if($wallet){
                foreach ($wallet as $key => $val) {
                    $type= $this->getTypeAttr($val['type']);
                    $wallet[$key]['type']=lang($type);
                }
            }

            if(count($wallet) != 0){
                return array('status'=>1,'data'=>$wallet);
            }else{
                return array('status'=>0);
            }
        }
    }
    private function getTypeAttr($value)
    {
        $status = [
            1 => '直推奖',2=>'申请节点退回', 3=>'申请超级节点退回',4=>'申请社区退回',5=>'申请节点扣除', 6=>'申请超级节点扣除',7=>'申请社区扣除',8=>'购买商品扣除',9=>'卖出商品所得',10=>'后台增加',11=>'后台减少'
        ];
        return $status[$value];
    }
    /* GMOD */
    public function  integral(){
        //GMOD记录
        $integral = integralLoglModel::where('userid',session('userid'))->order('id desc')->limit(5)->select();
        $this->assign('integral',$integral);
		return $this->fetch(); 
    }
    /**
     * 瀑布流加载GMOD记录
     */
    public function integralLog(Request $request){
        if($request->isPost()){
            $page = $request->param('page');
            $limit1 = ($page - 1)  * 5;
            $limit2 = 5;
            //GMOB记录
            $integral = DB::name('integral_log')->where('userid',session('userid'))->order('id desc')->limit($limit1,$limit2)->select();
            if($integral){
                foreach ($integral as $key => $val) {
                    $type= $this->getType($val['type']);
                    $integral[$key]['type']=lang($type);
                    $integral[$key]['create_time']=date("Y-m-d H:i:s", $integral[$key]['create_time']);
                }
            }
            if(count($integral) != 0){

                return array('status'=>1,'data'=>$integral);
            }else{
                return array('status'=>0);
            }
        }
    }
    public function getType($value)
    {

        $status = [
            1=>'直推奖',2=>'辅导奖',3=>'申请节点退回', 4=>'申请超级节点退回',5=>'申请社区退回',6=>'申请节点扣除', 7=>'申请超级节点扣除',8=>'申请社区扣除',9=>'节点奖',10=>'超级节点奖',11=>'社区奖',12=>'加权分红奖',13=>'购买商品扣除',14=>'卖出商品所得',15=>'见点奖',16=>'后台增加',17=>'后台减少'
        ];
        return $status[$value];
    }

    /**
     * 商品列表
     */
	public function search(Request $request){
        $cat_id = $request->param('cat_id') ? $request->param('cat_id') : null;
        $good_name = $request->param('good_name') ? $request->param('good_name') : null;
        $good_name = trim($good_name);
        //$this->history($good_name);
        $where = array();
        $where['is_on'] = 1;
        $order = 'id desc';     //排序字段
        if($cat_id){
            $where['cat_id']=$cat_id;
        }
        $goods_list = goodsModel::where($where)
            ->where('name','like','%'.$good_name.'%')
            ->where('amount','>',0)
            ->where('bis_id','<>',0)
            ->order($order)->select();

        foreach($goods_list as $key=>$row){
            $img = explode(',',$goods_list[$key]['img1']);
            $goods_list[$key]['img1'] = $img[0];
        }
        $count = count($goods_list);
        $this->assign('good_name',$good_name);
        $this->assign('count',$count);
        $this->assign('goods_list',$goods_list);
		return $this->fetch(); 
    }
    /* GMOD */
    public function  xingji_shouyi(){
        //GMOD记录
        $xingji_shouyi = integralLoglModel::where('userid',session('userid'))->where('type','in','1,2,12')->order('id desc')->limit(10)->select();
        $this->assign('xingji_shouyi',$xingji_shouyi);
        return $this->fetch();
    }
    /* GMOD */
    public function  juxing_shouyi(){
        //GMOD记录
        $juxing_shouyi = integralLoglModel::where('userid',session('userid'))->where('type',15)->order('id desc')->limit(10)->select();
        $this->assign('juxing_shouyi',$juxing_shouyi);
        return $this->fetch();
    }
    /**
     * 瀑布流加载GMOD记录
     */
    public function log(Request $request){
        if($request->isPost()){
            $page = $request->param('page');
            $type = $request->param('type');
            $limit1 = ($page - 1)  * 5;
            $limit2 = 5;
            if($type ==1){
                //奖励记录
                $shouyi = DB::name('integral_log')->where('userid',session('userid'))->where('type','in','1,2,12')->order('id desc')->limit($limit1,$limit2)->select();
                if($shouyi){
                    foreach ($shouyi as $key => $val) {
                        $type= $this->getType($val['type']);
                        $shouyi[$key]['type']=lang($type);
                        $shouyi[$key]['create_time']=date("Y-m-d H:i:s", $shouyi[$key]['create_time']);
                    }
                }
            }elseif($type ==2){
                //奖励记录
                $shouyi = DB::name('integral_log')->where('userid',session('userid'))->where('type',15)->order('id desc')->limit($limit1,$limit2)->select();
                if($shouyi){
                    foreach ($shouyi as $key => $val) {
                        $type= $this->getType($val['type']);
                        $shouyi[$key]['type']=lang($type);
                        $shouyi[$key]['create_time']=date("Y-m-d H:i:s", $shouyi[$key]['create_time']);
                    }
                }
            }

            if(count($shouyi) != 0){

                return array('status'=>1,'data'=>$shouyi);
            }else{
                return array('status'=>0);
            }
        }
    }
    /**
     * 瀑布流加载商品
     */
/*    public function search_goods(Request $request){
        if($request->isPost()){
            $page = $request->param('page');
            $limit1 = ($page - 1)  * 4;
            $limit2 = 4;
            $good_name = $request->param('good_name') ? $request->param('good_name') : null;
            $where = array();
            $where['is_on'] = 1;
            $order = 'id desc';
            $goods_list = goodsModel::where($where)
                ->where('name','like','%'.$good_name.'%')
                ->where('amount','>',0)
                ->limit($limit1,$limit2)
                ->order($order)->select();
            foreach($goods_list as $key=>$row){
                $img = explode(',',$goods_list[$key]['img1']);
                $goods_list[$key]['img1'] = $img[0];
            }
            if(count($goods_list) != 0){
                return array('status'=>1,'data'=>$goods_list);
            }else{
                return array('status'=>0);
            }
        }
    }*/
}